package com.example.lss.news.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lss.news.R;
import com.example.lss.news.WebViewActivity;
import com.example.lss.news.bean.Data;
import com.example.lss.news.utils.pic.GlideApp;

import java.util.List;
import java.util.logging.Handler;

/**
 * 主界面适配器
 */
public class ReAdapter extends RecyclerView.Adapter<ReAdapter.ViewHoldet> {

    private List<Data> list;

    public ReAdapter(List<Data> list) {
        this.list = list;
    }

    private ViewGroup context;
    private int width;

    @Override
    public ViewHoldet onCreateViewHolder(ViewGroup parent, int viewType) {

        context = parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.re_view, parent, false);
        width = parent.getMeasuredWidth();
        return new ViewHoldet(view);
    }

    @Override
    public void onBindViewHolder(ViewHoldet holder, int position) {
        Data data = list.get(position);
        holder.setData(data);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHoldet extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView tv, baoshe, time;
        private ImageView iv1, iv2, iv3;
        private LinearLayout ll;
        Data d;
        android.os.Handler handler = new android.os.Handler();

        public ViewHoldet(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.re_view_tv);
            baoshe = itemView.findViewById(R.id.re_view_baoshe);
            time = itemView.findViewById(R.id.re_view_time);
            ll = itemView.findViewById(R.id.ll);

            iv1 = itemView.findViewById(R.id.re_view_iv1);
            iv2 = itemView.findViewById(R.id.re_view_iv2);
            iv3 = itemView.findViewById(R.id.re_view_iv3);

            ll.setOnClickListener(this);

        }

        public void setData(final Data data) {
            tv.setText(data.getTitle());
            iv1.setImageResource(R.mipmap.placehold);
            iv2.setImageResource(R.mipmap.placehold);
            iv3.setImageResource(R.mipmap.placehold);


            new Thread() {
                @Override
                public void run() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {

                            GlideApp.with(context).load(data.getThumbnail_pic_s()).error(R.color.write).into(iv1);
                            GlideApp.with(context).load(data.getThumbnail_pic_s02()).error(R.color.write).into(iv2);
                            GlideApp.with(context).load(data.getThumbnail_pic_s03()).error(R.color.write).into(iv3);

                        }
                    });


                }
            }.start();


            baoshe.setText(data.getAuthor_name());
            time.setText(data.getDate());
            d = data;


        }

        @Override
        public void onClick(View view) {
            Intent it = new Intent(context.getContext(), WebViewActivity.class);
            it.putExtra("url", d.getUrl());
            context.getContext().startActivity(it);
        }
    }
}
